package cn.jingdalei.org.controller;

import cn.jingdalei.basic.util.AjaxResult;
import cn.jingdalei.basic.util.PageList;
import cn.jingdalei.org.domain.Department;
import cn.jingdalei.org.query.DepartmentQuery;
import cn.jingdalei.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

// @Controller
@RestController  // (controller + responseBoby[返回json格式] )
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 添加或者修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody  Department department){
        try {
            if (department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            // 避免多次创建对象
            return  AjaxResult.me().setMessage("操作失败");
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程思想
            return AjaxResult.me().setMessage("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程思想
            return AjaxResult.me().setMessage("删除失败");
        }
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询",notes = "如果有id就是修改否则就是添加")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     *  查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部部门信息",notes = "如果有id就是修改否则就是添加")
    public List<Department> findAll(){
        return departmentService.queryAll();
    }

    /**
     * 分页查询
     * axios get不支持传对象
     * @param query
     * @return
     */
    @PostMapping         // @GetMapping()不能传对象
    @ApiOperation(value = "分页查询部门信息",notes = "如果有id就是修改否则就是添加")
    public PageList<Department> findPage(@RequestBody DepartmentQuery query, HttpServletRequest req){
        // 测试前端携带过来的token
        System.out.println(req.getHeader("token"));//f6cd633f-9759-4e85-99d2-d6e554ab9673
        return departmentService.queryData(query);
    }

    /**
     * 加载上级部门树
     * @return
     */
    @GetMapping("/tree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
